// src/utils/urlUtils.ts
function getUrl(path: string): string {
  // 去掉路径中的 '@' 符号
  const relativePath = path.replace('@', '../')
  // 使用 import.meta.url 和 new URL 解析路径
  return new URL(relativePath, import.meta.url).href
}

/**
 * Checks if a given value is a valid number.
 * @param value - The value to be checked.
 * @returns Returns `true` if the value is a valid number, `false` otherwise.
 */
function isNumber(value: any): boolean {
  return typeof value === 'number' && !isNaN(value)
}

/**
 * Generates a random integer between the specified minimum and maximum values (inclusive).
 * @param min - The minimum value.
 * @param max - The maximum value.
 * @returns Returns a random integer between the minimum and maximum values.
 */
function getRandomIntBetween(min: number, max: number) {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

/**
 * Checks if a given value is a function.
 * @param val  - The value to be checked.
 * @returns Returns `true` if the value is a function, `false` otherwise.
 */
const isFunction = (val: unknown): val is Function => typeof val === 'function'

/**
 * Checks if a given value is a boolean.
 * @param val - The value to be checked.
 * @returns Returns `true` if the value is a boolean, `false` otherwise.
 */
const isBoolean = (val: unknown): val is boolean => typeof val === 'boolean'

/**
 * Checks if a given value is a string.
 * @param val - The value to be checked.
 * @returns Returns `true` if the value is a string, `false` otherwise.
 */
const isString = (val: unknown): val is string => typeof val === 'string'

/**
 * Checks if a given value is a symbol.
 * @param val  - The value to be checked.
 * @returns  Returns `true` if the value is a symbol, `false` otherwise.
 */
const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'

/**
 * Checks if a given value is an object.
 * @param val - The value to be checked.
 * @returns Returns `true` if the value is an object, `false` otherwise.
 */
const isObject = (val: unknown): val is Record<any, any> =>
  val !== null && typeof val === 'object'

/**
 * checks if a given value is a URL. 正则表达式匹配URL的模式
 * @param val - The value to be checked.
 */
const isUrl = (val: unknown): val is URL => {
  const urlPattern =
    /^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?$/
  return isString(val) && urlPattern.test(val)
}
/**
 * Checks if two objects are equal.
 * @description 深度比较两个对象是否相等
 * @param t - The first object to compare.
 * @param e - The second object to compare.
 * @returns  Returns `true` if the objects are equal, `false` otherwise.
 */
function mt(t, e) {
  if (t === e) return !0
  if (typeof t != 'object' || typeof e != 'object' || t == null || e == null)
    return !1
  let n = Object.keys(t),
    r = Object.keys(e)
  if (n.length !== r.length) return !1
  for (let o of n) if (!r.includes(o) || !mt(t[o], e[o])) return !1
  return !0
}
/**
 * check if two arrays are equal.
 * @param t - The first array to compare.
 * @param e - The second array to compare.
 * @returns Returns `true` if the arrays are equal, `false` otherwise.
 */
function It(t, e) {
  if (!t || !e) return !1
  let { length: n } = t
  if (n !== e.length) return !1
  for (let r = 0; r < n; r++) if (!Ct(t[r], e[r])) return !1
  return !0
}
/**
 * check if two are equal.
 * @param t - The first to compare.
 * @param e - The second to compare.
 * @returns Returns `true` if the two are equal, `false` otherwise.
 */
function Ct(t, e) {
  let n = Object.prototype.toString.call(t)
  return n !== Object.prototype.toString.call(e)
    ? !1
    : n === '[object Object]'
      ? mt(t, e)
      : n === '[object Array]'
        ? It(t, e)
        : n === '[object Function]'
          ? t === e
            ? !0
            : t.toString() === e.toString()
          : t === e
}
/**
 * check if two are equal.
 * @param str
 * @returns
 */
const isEqual = (t: unknown, e: unknown): boolean => {
  let n = Object.prototype.toString.call(t)
  return n !== Object.prototype.toString.call(e)
    ? !1
    : n === '[object Object]'
      ? mt(t, e)
      : n === '[object Array]'
        ? It(t, e)
        : n === '[object Function]'
          ? t === e
            ? !0
            : t.toString() === e.toString()
          : t === e
}

const nameHyphenate = (str: string) =>
  str.replace(/([A-Z])/g, '-').toLowerCase()

const debounce = (fn: Function, delay: number) => {
  let timer = null
  return function (this: any, ...args: any[]) {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fn.apply(this, args)
      timer = null
    }, delay)
  }
}

export {
  getUrl,
  isNumber,
  getRandomIntBetween,
  isFunction,
  isBoolean,
  isString,
  isSymbol,
  isObject,
  isUrl,
  isEqual,
  nameHyphenate,
  debounce
}
